﻿Imports System
Imports System.Drawing
Imports Gaia.Ajax.UI.WebControls.VideoPlayers
Imports Gaia.WebWidgets

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Private ReadOnly videos As Video() = New Video() {New Video(0, "Sky turning gray in short time", 3, "http://blip.tv/file/get/Behappy-clouds106.flv"), New Video(1, "Palm trees, big building and sun", 20, "http://blip.tv/file/get/KimAronson-TwentySeconds59483.flv"), New Video(2, "Colorful flowers and leaves.", 124, "http://blip.tv/file/get/N8inpasadena-Flowers457.flv")}

    Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
        pnlModal.Aspects.Add(New AspectModal(Color.Black, 0.5))

        If Not IsPostBack Then
            drpVideos.DataSource = videos
            drpVideos.DataValueField = "ID"
            drpVideos.DataTextField = "Name"
            drpVideos.DataBind()
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            SetCurrentVideo()
        End If
    End Sub

    Private Sub SetCurrentVideo()
        lblCurrentVideo.Text = videos(drpVideos.SelectedIndex).Name
        flowplayer.VideoFile = videos(drpVideos.SelectedIndex).Url
        flowplayer.ControlsOverVideo = chkControlsOverVideo.Checked
        flowplayer.ShowFullScreenButton = chkAllowFullscreen.Checked
        flowplayer.AutoPlay = chkAutoPlay.Checked

        If drpVideos.SelectedIndex = 2 Then
            ' add up a CuePoint for the last video. 
            flowplayer.AddCueuePoint(New CuePoint("ShowPanel", New TimeSpan(0, 0, 10)))
            flowplayer.AddCueuePoint(New CuePoint("ShowInfo", New TimeSpan(0, 0, 5)))
        End If

    End Sub

    Protected Sub PlayVideo(ByVal sender As Object, ByVal e As EventArgs)
        flowplayer.Play()
    End Sub

    ''' <summary>
    ''' FlowPlay dispatches this event when the state of the Player changes
    ''' </summary>
    Protected Sub flowplayer_StateChanged(ByVal sender As Object, ByVal e As FlowPlayer.StatusChangedEventArgs)
        lblStatus.Text = IIf(e.CurrentState = flowplayer.PlayerState.Paused, "Video is paused", "")
    End Sub

    ''' <summary>
    ''' Dispatched when a cue point is reached
    ''' </summary>
    Protected Sub flowplayer_CuePointReached(ByVal sender As Object, ByVal e As FlowPlayer.CuePointReachedEventArgs)
        Select Case e.CuePoint.Name
            Case "ShowPanel"
                pnlModal.Visible = True
                flowplayer.Pause()
                Exit Select

            Case "ShowInfo"
                lblStatus.Text = "This is informational text only and is display at CuePoint after 5 seconds"
                Dim effect As New Effect(lblStatus, effect.TypeOfEffect.Highlight)

                Exit Select
        End Select

    End Sub

    ''' <summary>
    ''' Dispatched when a video clip is finished
    ''' </summary>
    Protected Sub flowplayer_VideoClipFinished(ByVal sender As Object, ByVal e As EventArgs)
        ' Playlist logic can be kept server side. 
        Dim idx As Integer = drpVideos.SelectedIndex
        drpVideos.SelectedIndex = IIf((idx >= 0 AndAlso idx < (videos.Length - 1)), System.Threading.Interlocked.Increment(idx), 0)
        SetCurrentVideo()
        flowplayer.UpdatePlayer()
    End Sub

    Protected Sub ClosePanel(ByVal sender As Object, ByVal e As EventArgs)
        pnlModal.Visible = False
        flowplayer.Play()
    End Sub

    Protected Sub UpdatePlayer(ByVal sender As Object, ByVal e As EventArgs)
        SetCurrentVideo()
        flowplayer.UpdatePlayer()
    End Sub

   
End Class


''' <summary>
''' A Video
''' </summary>
Public Class Video
    Private m_name As String
    Private m_length As Integer
    Private m_url As String

    Private m_id As Integer
    Public Property ID() As Integer
        Get
            Return m_id
        End Get
        Set(ByVal value As Integer)
            m_id = value
        End Set
    End Property

    ''' <summary>
    ''' Name of Video
    ''' </summary>
    Public Property Name() As String
        Get
            Return m_name
        End Get
        Set(ByVal value As String)
            m_name = value
        End Set
    End Property

    ''' <summary>
    ''' Length of Video in seconds
    ''' </summary>
    Public Property Length() As Integer
        Get
            Return m_length
        End Get
        Set(ByVal value As Integer)
            m_length = value
        End Set
    End Property

    ''' <summary>
    ''' Url of Video
    ''' </summary>
    Public Property Url() As String
        Get
            Return m_url
        End Get
        Set(ByVal value As String)
            m_url = value
        End Set
    End Property

    Public Sub New(ByVal id As Integer, ByVal name As String, ByVal length As Integer, ByVal url As String)
        Me.m_id = id
        Me.m_name = name
        Me.m_length = length
        Me.m_url = url
    End Sub
End Class
